﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Web;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;
namespace DemoExtNet.ModuleNsToChuc
{
    public partial class PageNsTochuc : BusinessPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                NSToChuc nsToChuc = new NSToChuc();
                gridPanel1.SetDataSource(nsToChuc.GetAll());
            }
        }

        public override void InitializeControls()
        {
            base.InitializeControls();
            
            gridPanel1.AddGridColumn("TenToChucShort", "Tên Tổ Chức (Short)", 200);
            gridPanel1.AddGridColumn("TenToChucFull", "Tên Tổ Chức Full", 200);
            gridPanel1.AddGridColumn("GhiChu", "Ghi Chú", 200);
            gridPanel1.SetAutoExpandColumn("TenToChucShort");
            SetRecordField();
        }

        public override void LoadData()
        {
            //Viewer.BusinessObject.BusinessEntities.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
            ////Load xếp hạng
            //cboXepHang.SetDataSource(common.LoadDataCommonByCodeKey(common.XepHangDonVi));
            ////Load Khoi
            //cboKhoi.SetDataSource(common.LoadDataCommonByCodeKey(common.KhoiQuanlyToChuc));
            ////Loai To Chuc
            //cboLoaiToChuc.SetDataSource(common.LoadDataCommonByCodeKey(common.LoaiToChuc));
        }

        private void SetRecordField()
        {
            gridPanel1.AddRecordField(new Ext.Net.RecordField("ID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("ToChucID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("TenToChucShort", Ext.Net.RecordFieldType.String));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("TenToChucFull", Ext.Net.RecordFieldType.String));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("ToChucIDCha", Ext.Net.RecordFieldType.String));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("ToChucLevel", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("KhoiQuanLyID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("XepHangDonViID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("DiaChiID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("DonViHanhChinhTruocThuocID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("LoaiToChucID", Ext.Net.RecordFieldType.Int));
            gridPanel1.AddRecordField(new Ext.Net.RecordField("GhiChu", Ext.Net.RecordFieldType.String));            
        }

        public override void AddEvent()
        {
            UCSimpleDetailBase1.ShowDialog(0, Viewer.Base.Config.ViewerConstant.DetailsFormType.Add); 
        }

        public override void DeleteEvent(int id)
        {
            //UCSimpleDetailBase1.ShowDialog(0, Viewer.Base.Config.ViewerConstant.DetailsFormType.Add);
            UCSimpleDetailBase1.DoDelete(id);
        }

        public override void EditEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.Edit);
        }

        public override void DetailEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.View);
        }

        public override void CopyEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.Copy);
        }

        public override void Search()
        {
            string strWhere = string.Empty;
            if (this.cboXepHang.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("XepHangDonViID={0}", this.cboXepHang.SelectedItem.Value);
            }

            if (this.cboKhoi.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("KhoiQuanLyID = {0}", this.cboKhoi.SelectedItem.Value);
            }

            if (this.cboLoaiToChuc.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("LoaiToChucID = {0}", this.cboLoaiToChuc.SelectedItem.Value);
            }

            if (!string.IsNullOrEmpty(this.txtTenToChuc.Text.Trim()))
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("TenToChucShort.Contains(\"{0}\")", this.txtTenToChuc.Text.Trim());
            }

            Viewer.BusinessObject.BusinessEntities.NSToChuc tochucs = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
            List<Viewer.BusinessObject.BusinessEntities.NSToChuc> nsToChucs = tochucs.Search(strWhere);
            SetRecordField();
            gridPanel1.SetDataSource(nsToChucs);
        }

        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            this.Search();
        }


    }
}
